package com.yaohe.consumer.network;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;
import org.apache.commons.net.ftp.FTPReply;

public class FTPTransfer {
	
	public static boolean downloadPic(String name, int type, OutputStream output) {
		boolean res = true;
		
		FTPClient ftp;
     
        ftp = new FTPClient();
        
        try
        {
            int reply;
            ftp.connect(FtpConfigure.server);
           
            // After connection attempt, you should check the reply code to verify
            // success.
            reply = ftp.getReplyCode();

            if (!FTPReply.isPositiveCompletion(reply))
            {
                ftp.disconnect();
                return false;
            }
        }
        catch (IOException e)
        {
            if (ftp.isConnected())
            {
                try
                {
                    ftp.disconnect();
                }
                catch (IOException f)
                {
                    // do nothing
                }
            }
            return false;
        }

        try
        {
            if (!ftp.login(FtpConfigure.username, FtpConfigure.password))
            {
                ftp.logout();
                res = false;
            }

            ftp.setFileType(FTP.BINARY_FILE_TYPE);

            // Use passive mode as default because most of us are
            // behind firewalls these days.
            ftp.enterLocalPassiveMode();
            
            switch(type){
            case FtpConfigure.LOGO_TYPE:
            	ftp.retrieveFile(FtpConfigure.LOGO_PREFIX+name, output);
            	break;
            case FtpConfigure.PIC_TYPE:
            	ftp.retrieveFile(FtpConfigure.PIC_PREFIX+name, output);
            	break;
            default:
            	res = false;
            }
            output.close();
            ftp.logout();
        }
        catch (FTPConnectionClosedException e)
        {
            res = false;
        }
        catch (IOException e)
        {
            res = false;
        }
        finally
        {
            if (ftp.isConnected())
            {
                try
                {
                    ftp.disconnect();
                }
                catch (IOException f)
                {
                    // do nothing
                }
            }
        }

        return res;
	}
	
	public static boolean uploadPic(String name, int type, InputStream input) {
		boolean res = true;
		
		FTPClient ftp;
     
        ftp = new FTPClient();
        
        try
        {
            int reply;
            ftp.connect(FtpConfigure.server);
           
            // After connection attempt, you should check the reply code to verify
            // success.
            reply = ftp.getReplyCode();

            if (!FTPReply.isPositiveCompletion(reply))
            {
                ftp.disconnect();
                return false;
            }
        }
        catch (IOException e)
        {
            if (ftp.isConnected())
            {
                try
                {
                    ftp.disconnect();
                }
                catch (IOException f)
                {
                    // do nothing
                }
            }
            return false;
        }

        try
        {
            if (!ftp.login(FtpConfigure.username, FtpConfigure.password))
            {
                ftp.logout();
                res = false;
            }

            ftp.setFileType(FTP.BINARY_FILE_TYPE);

            // Use passive mode as default because most of us are
            // behind firewalls these days.
            ftp.enterLocalPassiveMode();
            
            switch(type){
            case FtpConfigure.LOGO_TYPE:
            	ftp.storeFile(FtpConfigure.LOGO_PREFIX+name, input);
            	break;
            case FtpConfigure.PIC_TYPE:
            	ftp.storeFile(FtpConfigure.PIC_PREFIX+name, input);
            	break;
            default:
            	res = false;
            }
            input.close();
            ftp.logout();
        }
        catch (FTPConnectionClosedException e)
        {
            res = false;
        }
        catch (IOException e)
        {
            res = false;
        }
        finally
        {
            if (ftp.isConnected())
            {
                try
                {
                    ftp.disconnect();
                }
                catch (IOException f)
                {
                    // do nothing
                }
            }
        }

        return res;
	}
}